package cn.edu.lit.myexpress.util;

import android.text.TextUtils;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cn.edu.lit.myexpress.db.ExpressConpany;
import cn.edu.lit.myexpress.gson.exp.ExpinfoList;
import cn.edu.lit.myexpress.gson.exp.Logistics;

public class Utility {




    //解析快递公司ID的json数据  并存入数据库中
    public  static boolean handleExpCompanyInfo(String response){

        if (!TextUtils.isEmpty(response)) {
            try {
                JSONObject obj = new JSONObject(response);
                String resultcode = obj.getString("resultcode");
                if (resultcode != null && resultcode.equals("200")) {
                    String result = obj.getString("result");
                    JSONArray arr = new JSONArray(result);
                    for (int i = 0; i < arr.length(); i++) {
                        JSONObject expcObj = arr.getJSONObject(i);
                        ExpressConpany expc = new ExpressConpany();
                        expc.setCompanyname(expcObj.getString("com"));
                        expc.setNo(expcObj.getString("no"));
                        expc.save();
                    }
                    return true;
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    //解析服务器返回json物流信息
    public static Logistics handleLogisticResponse(String response){
        Logistics logistics = new Logistics();
        List<ExpinfoList> expinfoLists = new ArrayList();
        if (!TextUtils.isEmpty(response)){
            try {
                JSONObject obj  = new JSONObject(response);
                String resultCode = obj.getString("resultcode");
                String reason = obj.getString("reason");
                if(resultCode!=null&&resultCode.equals("200")){//返回成功
                    String result =obj.getString("result");
                    JSONObject resultobj = new JSONObject(result);
                    logistics.setResuultCode(resultCode);
                    logistics.setReson(reason);
                    logistics.setCompany(resultobj.getString("company"));
                    logistics.setCompanyid(resultobj.getString("com"));
                    logistics.setNo(resultobj.getString("status"));
                    logistics.setStatus_detail(resultobj.getString("status_detail"));
                    java.text.SimpleDateFormat f=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date =new Date();
                    String d = f.format(date);
                    logistics.setLast_update(d);//记录请求发出的时间
                    String list= resultobj.getString("list");
                    JSONArray array = new JSONArray(list);
                    for (int i=0;i<array.length();i++){
                        ExpinfoList expinfoList = new ExpinfoList();
                        JSONObject listobj = array.getJSONObject(i);
                        expinfoList.setDatetime(listobj.getString("datetime"));
                        expinfoList.setRemark(listobj.getString("remark"));
                        expinfoList.setZone(listobj.getString("zone"));
                        expinfoLists.add(expinfoList);
                    }
                    logistics.setExpinfoLists(expinfoLists);
                    return logistics;
                }if(resultCode!=null){//快递订单不存在或者过期
                    logistics.setResuultCode(resultCode);
                    logistics.setReson(reason);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    // 日期格式转化方法
    public  static  String getNowData(int inmanyday){
        java.util.Calendar c=java.util.Calendar.getInstance();
        java.text.SimpleDateFormat f=new java.text.SimpleDateFormat("yyyyMMdd");
        int now =Integer.parseInt(f.format(c.getTime()));
        now +=inmanyday;
        return "day_"+String.valueOf(now);

    }

}
